﻿#include "bizeditordialog.h"

BizEditorDialog::BizEditorDialog(QWidget *parent) :
    BaseEditorDialog(parent)
{
    part=m_getPart();
}

void BizEditorDialog::search()
{
    showSearchBox("name");
}

void BizEditorDialog::updateMenu()
{
    menu->clear();

    if(isEdit)
    {
        menu->addAction(QStringLiteral("已完成"),this,&BizEditorDialog::en_finish);
        menu->addSeparator();
        menu->addAction(QStringLiteral("撤销"),this,&BizEditorDialog::revert);
        menu->addAction(QStringLiteral("保存"),this,&BizEditorDialog::save);
        menu->addSeparator();
        menu->addAction(QStringLiteral("删除"),this,&BizEditorDialog::en_remove);
        menu->addSeparator();
        menu->addAction(QStringLiteral("退出编辑模式"),this,&BizEditorDialog::edit_off);
    }
    else
        generateMenu();
}

void BizEditorDialog::updateView()
{
    indicator = 1;
    model->setTable(table);
    model->setFilter(QString("date = '%1'").arg(Env::today));
    model->setSort(0,Qt::DescendingOrder);
    model->select();

    if(model->rowCount() < 1)
        updateViewMonth();

    setHeaderData();
}

void BizEditorDialog::updateViewMonth()
{
    indicator = 2;
    QString preDate=Env::month + "-01";
    QString postDate=Env::month + "-31";

    model->setTable(table);
    model->setFilter(QString("date >= '%1' and date <= '%2'").arg(preDate).arg(postDate));
    model->setSort(0,Qt::DescendingOrder);
    model->select();

    if(model->rowCount() < 1)
        updateViewAll();

    setHeaderData();
}

void BizEditorDialog::updateViewAll()
{
    indicator = 3;
    model->setTable(table);
    model->setSort(0,Qt::DescendingOrder);
    model->select();

    setHeaderData();
}

void BizEditorDialog::updateViewUnfinish()
{
    indicator = 4;
    model->setTable(table);
    model->setFilter(QString("status != '%1'").arg(ST_FINISH));
    model->setSort(0,Qt::DescendingOrder);
    model->select();

    if(model->rowCount() < 1)
        updateViewAll();

    setHeaderData();
}

void BizEditorDialog::cancelItemDirect(const QString &table,int id)
{
    if(getStatus(table,id) != ST_WAIT)
    {
        QMessageBox::warning(this,QStringLiteral("状态无效"),QStringLiteral("只有【等待处理】才可以取消！"));
        return;
    }

    QString host=getValue(table,"host",id);
    if(!host.isEmpty() && host != Env::hostName )
    {
        QMessageBox::warning(this,QStringLiteral("无权操作"),QStringLiteral("您无权操作他人的记录！"));
        return;
    }
    changeStatus(table,ST_CANCEL,id);

    switch (indicator) {
    case 1:
        updateView();
        break;
    case 2:
        updateViewMonth();
        break;
    case 3:
        updateViewAll();
        break;
    case 4:
        updateViewUnfinish();
        break;
    }
}

void BizEditorDialog::finishItemDirect(const QString &table, int id)
{
    QString status = getStatus(table,id);
    if(status == ST_FINISH)
        return;
    changeStatus(table,ST_FINISH,id);

    switch (indicator) {
    case 1:
        updateView();
        break;
    case 2:
        updateViewMonth();
        break;
    case 3:
        updateViewAll();
        break;
    case 4:
        updateViewUnfinish();
        break;
    }
}

void BizEditorDialog::removeItem(const QModelIndex &index)
{
    if(model->data(index).toString() != ST_WAIT)
    {
        QMessageBox::warning(this,QStringLiteral("状态无效"),QStringLiteral("只有【等待处理】才可以删除！"));
        return;
    }
    model->removeRow(index.row());
}

int BizEditorDialog::getId(const QString &table, const QString &uid) const
{
    QSqlQuery query;
    query.exec(QString("select id from %1 where uid='%2'")
               .arg(table)
               .arg(uid));
    query.next();
    return query.value(0).toInt();
}

void BizEditorDialog::changeStatus(const QString &table, const QString &status, int id)
{
    QSqlQuery query;
    query.exec(QString("update %1 set status = '%2' where id = '%3'")
               .arg(table)
               .arg(status)
               .arg(id));
}

QString BizEditorDialog::getStatus(const QString &table, int id) const
{
    QSqlQuery query;
    query.exec(QString("select status from %1 where id='%2'")
               .arg(table)
               .arg(id));
    query.next();
    return query.value(0).toString();
}

void BizEditorDialog::sendMsg(const QString &title, const QString &content)
{
    QByteArray data;
    QDataStream out(&data,QIODevice::WriteOnly);
    out << title << content ;

    QUdpSocket sender;
    sender.writeDatagram(data,QHostAddress("192.168.1.255"),SEND_PORT);
}
